package com.sdtv.basemodule.mvp.presenter;

import java.util.HashMap;

/**
 * @ProjectName: MediunFusion
 * @Package: com.sdtv.basemodule.mvp.presenter
 * @ClassName: PresenterStore
 * @Description: java类作用描述
 * @Author: 小郭同学
 * @CreateDate: 2019-06-19 15:51
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class PresenterStore<P extends BasePresenter> {

    public static final String DEFAULT_KEY = "PresnterStore.defaultkey";
    private HashMap<String, P> mMap = new HashMap<>();

    public final void put(String key, P presenter) {
        P oldPresenter = mMap.get(DEFAULT_KEY + ":" + key);
        if (null != oldPresenter) {
            oldPresenter.onCleared();
            mMap.remove(oldPresenter);
        }
        mMap.put(DEFAULT_KEY + ":" + key, presenter);

    }

    /**
     * 获取PRESENTER
     *
     * @param key
     * @return
     */
    public final P get(String key) {
        return mMap.get(DEFAULT_KEY + ":" + key);
    }

    /**
     * MAP的长度
     *
     * @return
     */
    public int getSize() {
        return mMap.size();
    }

    public HashMap<String, P> getMap() {
        return mMap;
    }

    /**
     * 清空MAP
     */
    public void clear() {
        for (P presenter : mMap.values()) {
            presenter.onCleared();
        }
        mMap.clear();
    }
}
